package com.ruoyi.project.lesson.lessoncode.controller;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.lesson.lesson.domain.Lesson;
import com.ruoyi.project.lesson.lesson.service.ILessonService;
import com.ruoyi.project.lesson.lessoncode.domain.LessonActivationCode;
import com.ruoyi.project.lesson.lessoncode.service.ILessonActivationCodeService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 课程激活码Controller
 *
 * @author shixiangwen
 * @date 2021-04-20
 */
@Controller
@RequestMapping("/lesson/lessoncode")
public class LessonActivationCodeController extends BaseController {

    private String prefix = "lesson/lessoncode";

    @Autowired
    private ILessonActivationCodeService lessonActivationCodeService;

    @Autowired
    ILessonService lessonService;

    @RequiresPermissions("lesson:lessoncode:view")
    @GetMapping("/{lessonId}")
    public String lessoncode(@PathVariable("lessonId") Long lessonId, ModelMap mmap) {
        mmap.put("lesson", lessonService.selectLessonById(lessonId));
        return prefix + "/lessoncode";
    }

    /**
     * 查询课程激活码列表
     */
    @RequiresPermissions("lesson:lessoncode:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(LessonActivationCode lessonActivationCode) {
        startPage();
        List<LessonActivationCode> list = lessonActivationCodeService.selectLessonActivationCodeList(lessonActivationCode);
        return getDataTable(list);
    }

    /**
     * 导出课程激活码列表
     */
    @RequiresPermissions("lesson:lessoncode:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(LessonActivationCode lessonActivationCode) {
        List<LessonActivationCode> list = lessonActivationCodeService.selectLessonActivationCodeList(lessonActivationCode);
        ExcelUtil<LessonActivationCode> util = new ExcelUtil<LessonActivationCode>(LessonActivationCode.class);
        return util.exportExcel(list, "lessoncode");
    }

    /**
     * 新增课程激活码
     */
    @GetMapping("/add/{lessonId}")
    public String add(@PathVariable("lessonId") Long lessonId, ModelMap mmap) {
        mmap.put("lessonId", lessonId);
        return prefix + "/add";
    }

    /**
     * 新增保存课程激活码
     */
    @RequiresPermissions("lesson:lessoncode:add")
    @Log(title = "课程激活码", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(LessonActivationCode lessonActivationCode) {
        int count = 0;
        String codes = lessonActivationCode.getCodes();
        String[] datas = codes.split("\n");
        Lesson lesson = lessonService.selectLessonById(lessonActivationCode.getLessonId());
        for (String data : datas) {
            if (StringUtils.isEmpty(data)) {
                continue;
            }
            LessonActivationCode model = new LessonActivationCode();
            model.setCode(data);
            model.setChannel(lesson.getChannel());
            model.setLessonId(lesson.getId());
            model.setCreatedTime(DateUtils.getNowDate());
            model.setStatus(0);
            lessonActivationCodeService.insertLessonActivationCode(model);
            count += 1;
        }
        return toAjax(count);
    }

    /**
     * 修改课程激活码
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap) {
        LessonActivationCode lessonActivationCode = lessonActivationCodeService.selectLessonActivationCodeById(id);
        mmap.put("lessonActivationCode", lessonActivationCode);
        return prefix + "/edit";
    }

    /**
     * 修改保存课程激活码
     */
    @RequiresPermissions("lesson:lessoncode:edit")
    @Log(title = "课程激活码", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(LessonActivationCode lessonActivationCode) {
        return toAjax(lessonActivationCodeService.updateLessonActivationCode(lessonActivationCode));
    }

    /**
     * 删除课程激活码
     */
    @RequiresPermissions("lesson:lessoncode:remove")
    @Log(title = "课程激活码", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(lessonActivationCodeService.deleteLessonActivationCodeByIds(ids));
    }
}
